﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace Packager
{
	public partial class FormMain : Form
	{
		public FormMain()
		{
			InitializeComponent();
		}

		private void FormMain_Load(object sender, EventArgs e)
		{
			string file = Milan.Settings.ProductUser.GetValue("LastPackagerFile");
			if (file != null && File.Exists(file))
				LoadPackageFile(file);
		}

		private void buttonOpen_Click(object sender, EventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			if (openFileDialog.ShowDialog() == DialogResult.OK)
			{
				textBoxPackageFile.Text = openFileDialog.FileName;
				LoadPackageFile(openFileDialog.FileName);
			}
		}
		private void LoadPackageFile(string fileName)
		{
			XmlSerializer xmlSerializer = new XmlSerializer(typeof(Milan.Deploy.Package));
			using (var stream = new StreamReader(fileName))
			{
				m_package = (Milan.Deploy.Package)xmlSerializer.Deserialize(stream);
				stream.Close();
			}
			textBoxPackageFile.Text = fileName;
			listBoxFiles.Items.Clear();
			foreach (var file in m_package.Files)
				listBoxFiles.Items.Add(file.ToString());
			listBoxLinks.Items.Clear();
			foreach (var link in m_package.Links)
				listBoxLinks.Items.Add(link.ToString());
			comboBoxVersionFile.Items.Clear();
			foreach (var file in m_package.Files)
			{
				int i = comboBoxVersionFile.Items.Add(file);
				if(m_package.VersionFile == file.ToString())
					comboBoxVersionFile.SelectedIndex = i;
			}
			textBoxOnlineScript.Text = m_package.Script;
			foreach (var server in m_package.Servers)
				listBoxServers.Items.Add(server);
			textBoxTestCommand.Text = m_package.TestCommand;
			textBoxInstallCommand.Text = m_package.InstallCommand;
			textBoxUinstallCommand.Text = m_package.UninstallCommand;
			textBoxOnlineScript.Text = m_package.Script;
		}
		private void SavePackageFile(string fileName)
		{
			m_package.Script = textBoxOnlineScript.Text;
			m_package.InstallCommand = textBoxInstallCommand.Text;
			m_package.TestCommand = textBoxTestCommand.Text;
			m_package.UninstallCommand = textBoxUinstallCommand.Text;
			m_package.VersionFile = comboBoxVersionFile.SelectedText;
			XmlSerializer xmlSerializer = new XmlSerializer(typeof(Milan.Deploy.Package));
			using (var file = File.Create(fileName))
			{
				xmlSerializer.Serialize(file, m_package);
				Milan.Settings.ProductUser.SetValue("LastPackagerFile", fileName);
				file.Close();
			}
		}
		private Milan.Deploy.Package m_package = new Milan.Deploy.Package();
		private void buttonAddFiles_Click(object sender, EventArgs e)
		{
			FormFile formFile = new FormFile();
			if (formFile.ShowDialog() == DialogResult.OK)
			{
				m_package.Files.Add(formFile.OurFile);
				listBoxFiles.Items.Add(formFile.OurFile);
				comboBoxVersionFile.Items.Add(formFile.OurFile);
			}
		}

		private void buttonEditFile_Click(object sender, EventArgs e)
		{
			if (listBoxFiles.SelectedIndex >= 0)
			{
				FormFile formFile = new FormFile();
				formFile.OurFile = (Milan.Deploy.Package.File)listBoxFiles.SelectedItem;
				if (formFile.ShowDialog() == DialogResult.OK)
				{
					listBoxFiles.Items[listBoxFiles.SelectedIndex] = formFile.OurFile;
					comboBoxVersionFile.Items[listBoxFiles.SelectedIndex] = formFile.OurFile;
					listBoxFiles.Refresh();
				}
			}
		}

		private void buttonAddLink_Click(object sender, EventArgs e)
		{
			FormLink formLink = new FormLink();
			if (formLink.ShowDialog() == DialogResult.OK)
			{
				m_package.Links.Add(formLink.OurLink);
				listBoxLinks.Items.Add(formLink.OurLink);
			}
		}

		private void buttonEditLink_Click(object sender, EventArgs e)
		{
			if (listBoxLinks.SelectedIndex >= 0)
			{
				FormLink formLink = new FormLink();
				formLink.OurLink = (Milan.Deploy.Package.Link)listBoxLinks.SelectedItem;
				if (formLink.ShowDialog() == DialogResult.OK)
				{
					listBoxLinks.Items[listBoxLinks.SelectedIndex] = formLink.OurLink;
					listBoxLinks.Refresh();
				}
			}
		}

		private void buttonDeleteFile_Click(object sender, EventArgs e)
		{
			if (listBoxFiles.SelectedIndex >= 0)
			{
				m_package.Files.Remove((Milan.Deploy.Package.File)listBoxFiles.SelectedItem);
				listBoxFiles.Items.RemoveAt(listBoxFiles.SelectedIndex);
				comboBoxVersionFile.Items.RemoveAt(listBoxFiles.SelectedIndex);
			}
		}

		private void buttonDeleteLink_Click(object sender, EventArgs e)
		{
			if (listBoxLinks.SelectedIndex >= 0)
			{
				m_package.Links.Remove((Milan.Deploy.Package.Link)listBoxLinks.SelectedItem);
				listBoxLinks.Items.RemoveAt(listBoxLinks.SelectedIndex);
			}
		}

		private void buttonPublish_Click(object sender, EventArgs e)
		{
			Milan.Deploy.Client client = new Milan.Deploy.Client(m_package);
			client.UploadEntireProduct();
		}

		private void buttonSave_Click(object sender, EventArgs e)
		{
			if (!String.IsNullOrEmpty(textBoxPackageFile.Text))
				SavePackageFile(textBoxPackageFile.Text);
		}

		private void buttonSaveAs_Click(object sender, EventArgs e)
		{
			SaveFileDialog saveFileDialog = new SaveFileDialog();
			saveFileDialog.FileName = textBoxPackageFile.Text;
			if (saveFileDialog.ShowDialog() == DialogResult.OK)
			{
				textBoxPackageFile.Text = saveFileDialog.FileName;
				SavePackageFile(textBoxPackageFile.Text);
			}
		}

		private void buttonAddServer_Click(object sender, EventArgs e)
		{
			FormServer formServer = new FormServer();
			if (formServer.ShowDialog() == DialogResult.OK)
			{
				m_package.Servers.Add(formServer.ServerName);
				listBoxServers.Items.Add(formServer.ServerName);
			}
		}

		private void buttonEditServer_Click(object sender, EventArgs e)
		{
			{
				FormServer formServer = new FormServer();
				formServer.ServerName = (string)listBoxLinks.SelectedItem;
				if (formServer.ShowDialog() == DialogResult.OK)
				{
					listBoxServers.Items[listBoxLinks.SelectedIndex] = formServer.ServerName;
					listBoxServers.Refresh();
				}
			}
		}

		private void buttonDeleteServer_Click(object sender, EventArgs e)
		{
			if (listBoxServers.SelectedIndex >= 0)
			{
				m_package.Servers.Remove((string)listBoxServers.SelectedItem);
				listBoxServers.Items.RemoveAt(listBoxLinks.SelectedIndex);
			}
		}

	}
}
